<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="配置组" :width="width" @ok="handleSubmit" destroyOnClose>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
  </BasicDrawer>
  <ConfigGroupDrawer @register="registerConfigGroupDrawer" @success="reload"/>
</template>
<script lang="ts" setup>
import { ref, useAttrs } from 'vue';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { BasicTable, TableAction } from '/@/components/Table';
import { useDrawer } from '/@/components/Drawer';
import ConfigGroupDrawer from './group/ConfigGroupDrawer.vue';
import { useGlobSetting } from '/@/hooks/setting';
import { columns, searchFormSchema } from './group/configGroup.data';
import { listConfigGroup, deleteConfigGroup, batchDeleteConfigGroup } from './group/configGroup.api';
import { useListPage } from '/@/hooks/system/useListPage';
import { useMessage } from '/@/hooks/web/useMessage';
// 声明Emits
const emit = defineEmits(['success', 'register']);
const attrs = useAttrs()

const width = ref<number | string>('50%')
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'config-group',
  tableProps: {
    title: '配置组',
    api: listConfigGroup,
    showTableSetting: false,
    showIndexColumn: false,
    bordered: false,
    striped: false,
    immediate: true,
    columns: columns,
    formConfig: {
      schemas: searchFormSchema,
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
const { createMessage } = useMessage();
const [registerConfigGroupDrawer, { openDrawer: openConfigGroupDrawer }] = useDrawer();
const glob = useGlobSetting();

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  setDrawerProps({ confirmLoading: false });
});


/**
 * 提交
 */
async function handleSubmit() {
  try {
    closeDrawer();
    emit('success');
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}

/**
* 新增事件
*/
function handleAdd() {
  openConfigGroupDrawer(true, { isUpdate: false });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteConfigGroup({ id: record.id }, reload);
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteConfigGroup({ ids: selectedRowKeys.value }, reload);
}

const handleEdit = (record) => {
  openConfigGroupDrawer(true, { isUpdate: true, record });
}

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '删除',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      // ifShow: record.sendStatus == 0
    }
  ]
}
</script>
